#!/usr/bin/python
# -*- coding: UTF-8 -*-
#

import os,optparse
import platform
import myExceptions

# 获得一个目录下所有的文件列表，包括子目录下的文件列表
def get_recursive_file_list(search_path):
    current_files = os.listdir(search_path)
    all_files = []
    for file_name in current_files:
        full_file_name = os.path.join(search_path,file_name)
        all_files.append(full_file_name)
        if os.path.isdir(full_file_name):
            next_level_files = get_recursive_file_list(full_file_name)
            all_files.extend(next_level_files)
    return all_files
    pass

# 指定目录下递归子目录精确查找
# 返回绝对路径
def MySearch_file(filename, search_path):
    # print "MySearch_file",search_path
    all_files = get_recursive_file_list(search_path)
    for file in all_files:
        # print os.path.basename(file)
        # print filename.decode('utf-8').encode('gb2312')
        # print (os.path.basename(file) == filename.decode('utf-8').encode('gb2312'))
        if platform.system() == 'Windows':
            if os.path.basename(file) == filename.decode('utf-8').encode("gb2312"):
                return file
        else:
            if os.path.basename(file) == filename:
                return file            
            pass
    MyExceptions.MyException("MySearch_file--can't find ",filename,search_path)
    pass
# if __name__ == '__main__':
    # MySearch_file('MyComponent.py',"D:\workstation\design\三国杀 对战卡 配置文件\《三国杀传奇》数据设计总表(新结构)\pythonScript")
    # print os.path.isfile("D:\workstation\design\三国杀 对战卡 配置文件\《三国杀传奇》数据设计总表(新结构)\pythonScript\MyFileSearch.py".decode('utf-8'))
    # pass